package com.gzcsqic.excelapp.controller;

import cn.hutool.json.JSONUtil;
import com.gzcsqic.excelapp.common.Constants;
import com.gzcsqic.excelapp.common.R;
import com.gzcsqic.excelapp.entity.PageData;
import com.gzcsqic.excelapp.entity.Record;
import com.gzcsqic.excelapp.service.DataService;
import com.gzcsqic.excelapp.service.RecordService;
import com.gzcsqic.excelapp.utils.FileUtil;
import com.gzcsqic.excelapp.utils.Office2PDF;
import com.gzcsqic.excelapp.utils.OsUtil;
import com.gzcsqic.excelapp.utils.PrintUtil;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@RequestMapping("data")
@RestController
@CrossOrigin
public class DataController {

    @Resource
    private DataService dataService;
    @Resource
    private RecordService recordService;

    @PostMapping("uploadPic")
    public R uploadPic(@RequestParam("files") MultipartFile[] files) throws IOException {
        for (MultipartFile file : files) {
            System.out.println(file.getOriginalFilename());
        }
        return R.success(dataService.savePic(files));
    }

    @PostMapping("export")
    public R export(@RequestBody PageData pageData,@RequestParam(value = "type",required = false) Integer type) throws IOException {
        System.out.println(JSONUtil.toJsonStr(pageData));
        if(type == null){
            dataService.export(pageData);
        }else{
            dataService.exportOne(pageData,type);
        }
        return R.success();
    }

    @GetMapping("download")
    public void fileDownload(@RequestParam("fileName")String fileName, HttpServletResponse response) {
        try {
            String realFileName = fileName + ".xlsx";
            String filePath = OsUtil.getPath()+Constants.EXCELTEMP;
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtil.setAttachmentResponseHeader(response, realFileName);
            FileUtil.writeBytes(filePath, response.getOutputStream());
        } catch (Exception e) {

        }
    }

    @GetMapping("print")
    public R print(@RequestParam("printerName") String printerName){
        String sourceFilePath = OsUtil.getPath()+Constants.EXCELTEMP;
        PrintUtil.printFile(sourceFilePath,printerName);
        return R.success();
    }

    @RequestMapping("/view")
    public void readFile(HttpServletResponse res) throws Exception {
        InputStream in = null;
        OutputStream out = null;
        String sourceFilePath = OsUtil.getPath()+Constants.EXCELTEMP;
        String filePath = Office2PDF.openOfficeToPDF(sourceFilePath).getAbsolutePath();
        try {
            if (filePath != null) {
                in = new FileInputStream(filePath);
            }
            res.setContentType("application/pdf");
            out = res.getOutputStream();
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = in.read(b)) != -1) {
                out.write(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }


    @PostMapping("save")
    public R save(@RequestBody PageData pageData, @RequestParam("recordId") Integer recordId){
        dataService.saveToDb(pageData,recordId);
        return R.success();
    }
    @GetMapping("getRecord")
    public R getRecord(@RequestParam("recordId") Integer recordId){
        return R.success(dataService.readFromDb(recordId));
    }

}
